package com.sxit.tdf.server.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxit.tdf.server.entity.Classes;
import com.sxit.tdf.server.entity.bo.DetailClassInfoBo;
import com.sxit.tdf.server.entity.bo.SimpleClassesBo;
import com.sxit.tdf.server.entity.bo.SimpleClassesInfoBo;
import com.sxit.tdf.server.entity.vo.SiftClassesInfoVo;
import com.sxit.tdf.server.exception.TdfException;
import com.sxit.tdf.server.mapper.ClassesMapper;
import com.sxit.tdf.server.service.IClassesService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * 服务实现类
 *
 * @author 橙子高
 * @since 2022-02-13
 */
@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes>
    implements IClassesService {

  @Override
  public List<SimpleClassesBo> getSimpleClassesList() {
    return baseMapper.getSimpleClassesList();
  }

  @Override
  public int getEarlyYear() {
    return baseMapper.getEarlyYear();
  }

  @Override
  public IPage<SimpleClassesBo> getClassList(SiftClassesInfoVo siftClassesInfoVo) {

    HashMap hashMap = JSONUtil.parse(siftClassesInfoVo).toBean(HashMap.class);

    Page<SimpleClassesBo> page =
        new Page<SimpleClassesBo>(siftClassesInfoVo.getCurrent(), siftClassesInfoVo.getPageSize());

    return baseMapper.getClassList(page, hashMap);
  }

  @Override
  public boolean deleteById(int id) {

    Boolean delete = baseMapper.isDelete(id);
    if (delete) {
      int i = baseMapper.deleteById(id);
      return i >= 1;
    } else {
      throw new TdfException("改班级下仍有学生存在");
    }
  }

  @Override
  public DetailClassInfoBo getDetailClassInfoBo(int id) {
    return baseMapper.getDetailClassInfoBo(id);
  }

  @Override
  public List<SimpleClassesInfoBo> getSimpleClassesInfoBo() {
    return baseMapper.getSimpleClassesInfoBo();
  }
}
